home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / BITSET.H < prev    next >
C/C++ Source or Header  |  1992-03-29  |  9KB  |  369 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _BitSet_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23.  
  24. #define _BitSet_h 1
  25.  
  26. #include <stream.h>
  27. #include <values.h>
  28.  
  29. #define BITSETBITS  BITS(short)
  30.  
  31. struct BitSetRep
  32. {
  33.   unsigned short  len;          // number of shorts in s
  34.   unsigned short  sz;           // allocated slots
  35.   unsigned short  virt;         // virtual 0 or 1
  36.   unsigned short  s[1];         // bits start here
  37. };
  38.  
  39. extern BitSetRep*   BitSetalloc(BitSetRep*, const unsigned short*, 
  40.                                 int, int, int);
  41. extern BitSetRep*   BitSetcopy(BitSetRep*, const BitSetRep*);
  42. extern BitSetRep*   BitSetresize(BitSetRep*, int);
  43. extern BitSetRep*   BitSetop(const BitSetRep*, const BitSetRep*, 
  44.                              BitSetRep*, char);
  45. extern BitSetRep*   BitSetcmpl(const BitSetRep*, BitSetRep*);
  46.  
  47.  
  48. extern BitSetRep    _nilBitSetRep;
  49.  
  50. class BitSet;
  51.  
  52. class BitSetBit
  53. {
  54. protected:
  55.   BitSet*            src;
  56.   unsigned long      pos;
  57.  
  58.  public:
  59.                      BitSetBit(BitSet* v, int p);
  60.                      BitSetBit(const BitSetBit& b);
  61.                     ~BitSetBit();
  62.                      operator int();
  63.   int                operator = (int b);
  64.   int                operator == (int b);
  65.   int                operator != (int b);
  66. };
  67.  
  68. class BitSet
  69. {
  70. protected:
  71.   BitSetRep*          rep;
  72.  
  73.   
  74. public:
  75.  
  76. // constructors
  77.                      BitSet();
  78.                      BitSet(const BitSet&);
  79.  
  80.                     ~BitSet();
  81.  
  82.   void               operator =  (const BitSet& y);
  83.  
  84. // equality & subset tests
  85.  
  86.   friend int         operator == (const BitSet& x, const BitSet& y);
  87.   friend int         operator != (const BitSet& x, const BitSet& y);
  88.   friend int         operator <  (const BitSet& x, const BitSet& y);
  89.   friend int         operator <= (const BitSet& x, const BitSet& y);
  90.   friend int         operator >  (const BitSet& x, const BitSet& y);
  91.   friend int         operator >= (const BitSet& x, const BitSet& y);
  92.  
  93.  
  94. // operations on self
  95.  
  96.   void               operator |= (const BitSet& y);
  97.   void               operator &= (const BitSet& y);
  98.   void               operator -= (const BitSet& y);
  99.   void               operator ^= (const BitSet& y);
  100.  
  101.   void               complement();
  102.  
  103. // individual bit manipulation
  104.  
  105.   void               set(int pos);
  106.   void               set(int from, int to);
  107.   void               set(); // set all
  108.  
  109.   void               clear(int pos);
  110.   void               clear(int from, int to);
  111.   void               clear(); // clear all
  112.  
  113.   void               invert(int pos);
  114.   void               invert(int from, int to);
  115.  
  116.   int                test(int pos) const;
  117.   int                test(int from, int to) const;
  118.  
  119.   BitSetBit          operator [] (int i);
  120.   
  121. // iterators
  122.  
  123.   int                first(int b = 1) const;
  124.   int                last(int b = 1) const;
  125.  
  126.   int                next(int pos, int b = 1) const;
  127.   int                previous(int pos, int b = 1) const;
  128.  
  129. // status
  130.  
  131.   int                empty() const;
  132.   int                virtual_bit() const;
  133.   int                count(int b = 1) const;
  134.   
  135. // convertors & IO
  136.  
  137.   friend BitSet      atoBitSet(const char* s, 
  138.                                char f='0', char t='1', char star='*');
  139.   friend const char* BitSettoa(const BitSet& x, 
  140.                                char f='0', char t='1', char star='*');
  141.  
  142.   friend BitSet      shorttoBitSet(unsigned short w);
  143.   friend BitSet      longtoBitSet(unsigned long w);
  144.  
  145.   friend ostream&    operator << (ostream& s, const BitSet& x);
  146.  
  147. // procedural versions of operators
  148.  
  149.   friend void        and(const BitSet& x, const BitSet& y, BitSet& r);
  150.   friend void        or(const BitSet& x, const BitSet& y, BitSet& r);
  151.   friend void        xor(const BitSet& x, const BitSet& y, BitSet& r);
  152.   friend void        diff(const BitSet& x, const BitSet& y, BitSet& r);
  153.   friend void        complement(const BitSet& x, BitSet& r);
  154.  
  155. // misc
  156.  
  157.   void      error(const char* msg) const;
  158.   int                OK() const;
  159. };
  160.  
  161.  
  162. typedef BitSet BitSetTmp;
  163.  
  164.  
  165.   BitSet      operator |  (const BitSet& x, const BitSet& y);
  166.   BitSet      operator &  (const BitSet& x, const BitSet& y);
  167.   BitSet      operator -  (const BitSet& x, const BitSet& y);
  168.   BitSet      operator ^  (const BitSet& x, const BitSet& y);
  169.  
  170.   BitSet      operator ~  (const BitSet& x);
  171.  
  172. // These are inlined regardless of optimization
  173.  
  174. inline int BitSet_index(int l)
  175. {
  176.   return (unsigned)(l) / BITSETBITS;
  177. }
  178.  
  179. inline int BitSet_pos(int l)
  180. {
  181.   return l & (BITSETBITS - 1);
  182. }
  183.  
  184.  
  185. inline BitSet::BitSet() : rep(&_nilBitSetRep) {}
  186.  
  187. inline BitSet::BitSet(const BitSet& x) :rep(BitSetcopy(0, x.rep)) {}
  188.  
  189. inline BitSet::~BitSet() { if (rep != &_nilBitSetRep) delete rep; }
  190.  
  191. inline void BitSet::operator =  (const BitSet& y)
  192.   rep = BitSetcopy(rep, y.rep);
  193. }
  194.  
  195. inline int operator != (const BitSet& x, const BitSet& y) { return !(x == y); }
  196.  
  197. inline int operator >  (const BitSet& x, const BitSet& y) { return y < x; }
  198.  
  199. inline int operator >= (const BitSet& x, const BitSet& y) { return y <= x; }
  200.  
  201. inline void and(const BitSet& x, const BitSet& y, BitSet& r)
  202. {
  203.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '&');
  204. }
  205.  
  206. inline void or(const BitSet& x, const BitSet& y, BitSet& r)
  207. {
  208.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '|');
  209. }
  210.  
  211. inline void xor(const BitSet& x, const BitSet& y, BitSet& r)
  212. {
  213.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '^');
  214. }
  215.  
  216. inline void diff(const BitSet& x, const BitSet& y, BitSet& r)
  217. {
  218.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '-');
  219. }
  220.  
  221. inline void complement(const BitSet& x, BitSet& r)
  222. {
  223.   r.rep = BitSetcmpl(x.rep, r.rep);
  224. }
  225.  
  226. #if defined(__GNUG__) && !defined(NO_NRV)
  227.  
  228. inline BitSet operator & (const BitSet& x, const BitSet& y) return r
  229. {
  230.   and(x, y, r);
  231. }
  232.  
  233. inline BitSet operator | (const BitSet& x, const BitSet& y) return r
  234. {
  235.   or(x, y, r);
  236. }
  237.  
  238. inline BitSet operator ^ (const BitSet& x, const BitSet& y) return r
  239. {
  240.   xor(x, y, r);
  241. }
  242.  
  243. inline BitSet operator - (const BitSet& x, const BitSet& y) return r
  244. {
  245.   diff(x, y, r);
  246. }
  247.  
  248. inline BitSet operator ~ (const BitSet& x) return r
  249. {
  250.   ::complement(x, r);
  251. }
  252.  
  253. #else /* NO_NRV */
  254.  
  255. inline BitSet operator & (const BitSet& x, const BitSet& y) 
  256. {
  257.   BitSet r; and(x, y, r); return r;
  258. }
  259.  
  260. inline BitSet operator | (const BitSet& x, const BitSet& y) 
  261. {
  262.   BitSet r; or(x, y, r); return r;
  263. }
  264.  
  265. inline BitSet operator ^ (const BitSet& x, const BitSet& y) 
  266. {
  267.   BitSet r; xor(x, y, r); return r;
  268. }
  269.  
  270. inline BitSet operator - (const BitSet& x, const BitSet& y) 
  271. {
  272.   BitSet r; diff(x, y, r); return r;
  273. }
  274.  
  275. inline BitSet operator ~ (const BitSet& x) 
  276. {
  277.   BitSet r; ::complement(x, r); return r;
  278. }
  279.  
  280. #endif
  281.  
  282. inline void BitSet::operator &= (const BitSet& y)
  283. {
  284.   and(*this, y, *this);
  285. }
  286.  
  287. inline void BitSet::operator |= (const BitSet& y)
  288. {
  289.   or(*this, y, *this);
  290. }
  291.  
  292. inline void BitSet::operator ^= (const BitSet& y)
  293. {
  294.   xor(*this, y, *this);
  295. }
  296.  
  297. inline void BitSet::operator -= (const BitSet& y)
  298. {
  299.   diff(*this, y, *this);
  300. }
  301.  
  302.  
  303. inline void BitSet::complement()
  304. {
  305.   ::complement(*this, *this);
  306. }
  307.  
  308. inline int BitSet::virtual_bit() const
  309. {
  310.   return rep->virt;
  311. }
  312.  
  313. inline int BitSet::first(int b) const
  314. {
  315.   return next(-1, b);
  316. }
  317.  
  318. inline int BitSet::test(int p) const
  319. {
  320.   if (p < 0) error("Illegal bit index");
  321.   int index = BitSet_index(p);
  322.   return (index >= rep->len)? rep->virt : 
  323.          ((rep->s[index] & (1 << BitSet_pos(p))) != 0);
  324. }
  325.  
  326.  
  327. inline void BitSet::set()
  328. {
  329.   rep = BitSetalloc(rep, 0, 0, 1, 0);
  330. }
  331.  
  332. inline BitSetBit::BitSetBit(const BitSetBit& b) :src(b.src), pos(b.pos) {}
  333.  
  334. inline BitSetBit::BitSetBit(BitSet* v, int p)
  335. {
  336.   src = v;  pos = p;
  337. }
  338.  
  339. inline BitSetBit::~BitSetBit() {}
  340.  
  341. inline BitSetBit::operator int()
  342. {
  343.   return src->test(pos);
  344. }
  345.  
  346. inline int BitSetBit::operator = (int b)
  347. {
  348.   if (b) src->set(pos); else src->clear(pos); return b;
  349. }
  350.  
  351. inline int BitSetBit::operator == (int b)
  352. {
  353.   return src->test(pos) == b;
  354. }
  355.  
  356. inline int BitSetBit::operator != (int b)
  357. {
  358.   return src->test(pos) != b;
  359. }
  360.  
  361. inline BitSetBit BitSet::operator [] (int i)
  362. {
  363.   if (i < 0) error("illegal bit index");
  364.   return BitSetBit(this, i);
  365. }
  366.  
  367. #endif
  368.